package com.yanqu.road.server.gameplayer.module.activity.coupon;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.coupon.CouponActivityUserData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.recharge.CouponBussiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;

import java.util.List;

public class CouponActivityModule extends GeneralModule {
    private CouponActivityUserData couponActivityUserData;

    public CouponActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.RechargeActivity.getValue(), 17);
        if(activityInfoList.size() > 0){
            ActivityInfo activityInfo = activityInfoList.get(0);
            if(null != activityInfo){
                couponActivityUserData = CouponBussiness.getCouponActivityUserData(activityInfo.getActivityId(), player.getUserId());
            }
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if(null != couponActivityUserData){
            if(couponActivityUserData.isInsertOption()){
                CouponBussiness.addCouponActivityUserData(couponActivityUserData);
            }else if(couponActivityUserData.isUpdateOption()){
                CouponBussiness.updateCouponActivityUserData(couponActivityUserData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        initUserData();
    }

    public void dailyReset(){
        if(null == couponActivityUserData){
            return;
        }
        couponActivityUserData.dailyReset();
    }

    public void initUserData(){
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.RechargeActivity.getValue(), 17);
        if(activityInfoList.size() > 0){
            ActivityInfo activityInfo = activityInfoList.get(0);
            if(null != activityInfo){
                initUserData(activityInfo.getActivityId());
            }
        }
    }

    private synchronized void initUserData(int activityId){
        if(null == couponActivityUserData || couponActivityUserData.getActivityId() != activityId){
            CouponActivityUserData userData = new CouponActivityUserData();
            userData.setActivityId(activityId);
            userData.setUserId(player.getUserId());
            userData.setInsertOption();
        }
    }

    public CouponActivityUserData getCouponActivityUserData(){
        return couponActivityUserData;
    }
}
